package com.mcf.xiaolinju.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.mcf.xiaolinju.entity.TbCity;
import com.mcf.xiaolinju.entity.TbProvince;
import com.mcf.xiaolinju.entity.TbTown;
import com.mcf.xiaolinju.mapper.TbCityMapper;
import com.mcf.xiaolinju.mapper.TbProvinceMapper;
import com.mcf.xiaolinju.service.ITbCityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mcf.xiaolinju.vo.TreeVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mcf
 * @since 2022-01-04
 */
@Service
public class TbCityServiceImpl extends ServiceImpl<TbCityMapper, TbCity> implements ITbCityService {
    @Resource
    TbCityMapper tbCityMapper;
    @Resource
    TbProvinceServiceImpl tbProvinceService;
    @Resource
    TbTownServiceImpl tbTownService;
    
    //根据省份id查询城市
    public List<TbCity> getcity(int provinceid){
        return  tbCityMapper.getcity(provinceid);
    }
    public List<TreeVo> gettree(){
        List<TreeVo> treeVos=new ArrayList<>();
        List<TbProvince> provincelist = tbProvinceService.list();
        for(TbProvince tbProvinces:provincelist){
            TreeVo treeVo=new TreeVo();
            treeVo.setText(tbProvinces.getPname());
            treeVo.setValue(String.valueOf(tbProvinces.getId()));
            List<TreeVo> treeVos2 = new ArrayList<>();
            List<TbCity> Citylist= tbCityMapper.getcity(tbProvinces.getId());
            if(!CollectionUtils.isEmpty(Citylist)){
                for(TbCity tbCitys:Citylist){
                    TreeVo treeVo2=new TreeVo();
                    treeVo2.setText(tbCitys.getCname());
                    treeVo2.setValue(tbProvinces.getId()+"-"+tbCitys.getId());
                    treeVos2.add(treeVo2);
                    List<TreeVo> treeVos3=new ArrayList<>();
                    List<TbTown> townlist=tbTownService.gettown(tbCitys.getId());
                    if(!CollectionUtils.isEmpty(townlist)){
                        for(TbTown tbTowns:townlist){
                            TreeVo treeVo3=new TreeVo();
                            treeVo3.setText(tbTowns.getTname());
                            treeVo3.setValue(tbProvinces.getId()+"-"+tbCitys.getId()+"-"+tbTowns.getId());
                            treeVos3.add(treeVo3);
                        }
                        treeVo2.setChildren(treeVos3);
                    }
                }
                treeVo.setChildren(treeVos2);
            }
            treeVos.add(treeVo);
        }

        return treeVos;
    }


}
